﻿using Model.Entites.Base;
using SqlSugar;
using System.Linq.Expressions;

namespace IService.Base
{
    public interface IBaseService<T> where T : ModelBase, new()
    {
        Task<RM_ApiResult> GetItem(int Key);

        Task<RM_PageReturn<T>> GetPageByExp(Expression<Func<T, bool>> exp, Par_AdminPageBase par, Expression<Func<T, object>> expression, OrderByType type = OrderByType.Asc);

        Task<RM_PageReturn<T>> GetPageHasInclueByExp(Expression<Func<T, bool>> exp, Par_AdminPageBase par, Expression<Func<T, object>> include, Expression<Func<T, object>> expression, OrderByType type = OrderByType.Asc);

        Task<RM_PageReturn<T1>> GetOtherPageByExp<T1>(Expression<Func<T1, bool>> exp, Par_AdminPageBase par, Expression<Func<T1, object>> expression, OrderByType type = OrderByType.Asc) where T1 : ModelBase, new();

        Task<int> AddOrUpdate(T par, bool NeedKey = false, Expression<Func<T, bool>> addLimit = null, Expression<Func<T, bool>> updateLimit = null);

        Task<RM_ApiResult> Status<T1>(int Key, int status) where T1 : ModelStatusBase, new();

        Task<RM_ApiResult> DeleteItem(int Key);

        Task<RM_ApiResult> DeleteByKeys(List<int> Keys);

        /// <summary>
        /// 数据库连接
        /// </summary>
        public ISqlSugarClient Db { get; }

        #region 扩展方法

        #region 新增

        /// <summary>
        /// 批量添加实体
        /// </summary>
        /// <param name="entities">要添加的实体列表</param>
        /// <returns>受影响的行数</returns>
        Task<int> Add(List<T> entities);

        /// <summary>
        /// 添加单个实体
        /// </summary>
        /// <param name="entity">要添加的实体</param>
        /// <returns>受影响的行数</returns>
        Task<int> Add(T entity);

        /// <summary>
        /// 添加实体并返回新增的实体对象
        /// </summary>
        /// <param name="entity">要添加的实体</param>
        /// <returns>新增的实体对象</returns>
        Task<T> AddREntity(T entity);

        /// <summary>
        /// 添加实体并返回自增ID
        /// </summary>
        /// <param name="entity">要添加的实体</param>
        /// <returns>自增ID</returns>
        Task<int> AddRId(T entity);

        #endregion 新增

        #region 新增2

        /// <summary>
        /// 批量添加实体
        /// </summary>
        /// <param name="entities">要添加的实体列表</param>
        /// <returns>受影响的行数</returns>
        Task<int> Add<T2>(List<T2> entities) where T2 : ModelBase, new();

        /// <summary>
        /// 添加单个实体
        /// </summary>
        /// <param name="entity">要添加的实体</param>
        /// <returns>受影响的行数</returns>
        Task<int> Add<T2>(T2 entity) where T2 : ModelBase, new();

        /// <summary>
        /// 添加实体并返回新增的实体对象
        /// </summary>
        /// <param name="entity">要添加的实体</param>
        /// <returns>新增的实体对象</returns>
        Task<T2> AddREntity<T2>(T2 entity) where T2 : ModelBase, new();

        /// <summary>
        /// 添加实体并返回自增ID
        /// </summary>
        /// <param name="entity">要添加的实体</param>
        /// <returns>自增ID</returns>
        Task<int> AddRId<T2>(T2 entity) where T2 : ModelBase, new();

        #endregion 新增2

        #region 删除

        /// <summary>
        /// 根据ID删除实体
        /// </summary>
        /// <param name="id">实体的ID</param>
        /// <returns>如果删除成功返回true，否则返回false</returns>
        Task<bool> Delete(int id);

        /// <summary>
        /// 根据ID列表删除实体
        /// </summary>
        /// <param name="ids">实体的ID列表</param>
        /// <returns>如果删除成功返回true，否则返回false</returns>
        Task<bool> Delete(List<int> ids);

        /// <summary>
        /// 根据条件表达式删除实体
        /// </summary>
        /// <param name="whereExpression">删除条件表达式</param>
        /// <returns>如果删除成功返回true，否则返回false</returns>
        Task<bool> Delete(Expression<Func<T, bool>> whereExpression);

        /// <summary>
        /// 根据SQL语句删除实体
        /// </summary>
        /// <param name="sql">删除的SQL语句</param>
        /// <returns>如果删除成功返回true，否则返回false</returns>
        Task<bool> DeleteSql(string sql);

        #endregion 删除

        #region 删除2

        /// <summary>
        /// 根据ID删除实体
        /// </summary>
        /// <param name="id">实体的ID</param>
        /// <returns>如果删除成功返回true，否则返回false</returns>
        Task<bool> Delete<T2>(int id) where T2 : ModelBase, new();

        /// <summary>
        /// 根据ID列表删除实体
        /// </summary>
        /// <param name="ids">实体的ID列表</param>
        /// <returns>如果删除成功返回true，否则返回false</returns>
        Task<bool> Delete<T2>(List<int> ids) where T2 : ModelBase, new();

        /// <summary>
        /// 根据条件表达式删除实体
        /// </summary>
        /// <param name="whereExpression">删除条件表达式</param>
        /// <returns>如果删除成功返回true，否则返回false</returns>
        Task<bool> Delete<T2>(Expression<Func<T2, bool>> whereExpression) where T2 : ModelBase, new();

        #endregion 删除2

        #region 查询

        /// <summary>
        /// 根据ID获取单个实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<T> Get(int id);

        /// <summary>
        /// 根据条件表达式获取单个实体
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        Task<T> Get(Expression<Func<T, bool>> whereExpression = null);

        /// <summary>
        ///
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        Task<int> Count(Expression<Func<T, bool>> whereExpression = null);

        /// <summary>
        /// 根据条件表达式获取实体列表
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        Task<List<T>> GetList(Expression<Func<T, bool>> whereExpression = null);

        /// <summary>
        /// 根据SQL语句获取实体列表
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        Task<List<T>> GetList(string sql);

        /// <summary>
        /// 是否存在这条记录
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        Task<bool> Any(Expression<Func<T, bool>> whereExpression = null);

        /// <summary>
        /// 根据ID判断记录是否存在
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<bool> Any(int id);

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderBy"></param>
        /// <param name="orderByType"></param>
        /// <returns></returns>
        Task<(List<T>, int, int)> Page(Expression<Func<T, bool>> expression = null, int pageIndex = 1, int pageSize = 15,
           Expression<Func<T, object>> orderBy = null, OrderByType orderByType = OrderByType.Asc);

        /// <summary>
        /// 根据SQL分页查询
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderBy"></param>
        /// <param name="orderByType"></param>
        /// <returns></returns>
        Task<(List<T>, int, int)> Page(string sql = null, int pageIndex = 1, int pageSize = 15,
           Expression<Func<T, object>> orderBy = null, OrderByType orderByType = OrderByType.Asc);

        #endregion 查询

        #region 查询2

        /// <summary>
        /// 根据ID获取单个实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<T2> Get<T2>(int id) where T2 : ModelBase, new();

        /// <summary>
        /// 根据条件表达式获取单个实体
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        Task<T2> Get<T2>(Expression<Func<T2, bool>> whereExpression = null) where T2 : ModelBase, new();

        /// <summary>
        ///
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        Task<int> Count<T2>(Expression<Func<T2, bool>> whereExpression = null) where T2 : ModelBase, new();

        /// <summary>
        /// 根据条件表达式获取实体列表
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        Task<List<T2>> GetList<T2>(Expression<Func<T2, bool>> whereExpression = null) where T2 : ModelBase, new();

        /// <summary>
        /// 根据SQL语句获取实体列表
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        Task<List<T2>> GetList<T2>(string sql) where T2 : ModelBase, new();

        /// <summary>
        /// 是否存在这条记录
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        Task<bool> Any<T2>(Expression<Func<T2, bool>> whereExpression = null) where T2 : ModelBase, new();

        /// <summary>
        /// 根据ID判断记录是否存在
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<bool> Any<T2>(int id) where T2 : ModelBase, new();

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderBy"></param>
        /// <param name="orderByType"></param>
        /// <returns></returns>
        Task<(List<T2>, int, int)> Page<T2>(Expression<Func<T2, bool>> expression = null, int pageIndex = 1, int pageSize = 15,
           Expression<Func<T2, object>> orderBy = null, OrderByType orderByType = OrderByType.Asc) where T2 : ModelBase, new();

        /// <summary>
        /// 根据SQL分页查询
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderBy"></param>
        /// <param name="orderByType"></param>
        /// <returns></returns>
        Task<(List<T2>, int, int)> Page<T2>(string sql = null, int pageIndex = 1, int pageSize = 15,
           Expression<Func<T2, object>> orderBy = null, OrderByType orderByType = OrderByType.Asc) where T2 : ModelBase, new();

        #endregion 查询2

        #region 查询3

        /// <summary>
        /// 根据ID获取单个实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<V> Get<T2, V>(int id) where T2 : ModelBase, new();

        /// <summary>
        /// 根据条件表达式获取单个实体
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        Task<V> Get<T2, V>(Expression<Func<T2, bool>> whereExpression = null) where T2 : ModelBase, new();

        /// <summary>
        /// 根据条件表达式获取实体列表
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        Task<List<V>> GetList<T2, V>(Expression<Func<T2, bool>> whereExpression = null) where T2 : ModelBase, new();

        /// <summary>
        /// 根据SQL语句获取实体列表
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        Task<List<V>> GetList<T2, V>(string sql) where T2 : ModelBase, new();

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderBy"></param>
        /// <param name="orderByType"></param>
        /// <returns></returns>
        Task<(List<V>, int, int)> Page<T2, V>(Expression<Func<T2, bool>> expression = null, int pageIndex = 1, int pageSize = 15,
           Expression<Func<T2, object>> orderBy = null, OrderByType orderByType = OrderByType.Asc) where T2 : ModelBase, new();

        /// <summary>
        /// 根据SQL分页查询
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderBy"></param>
        /// <param name="orderByType"></param>
        /// <returns></returns>
        Task<(List<V>, int, int)> Page<T2, V>(string sql = null, int pageIndex = 1, int pageSize = 15,
           Expression<Func<T2, object>> orderBy = null, OrderByType orderByType = OrderByType.Asc) where T2 : ModelBase, new();

        #endregion 查询3

        #region 更新

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<int> Update(T entity);

        /// <summary>
        /// 只更新不为null的字段
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<int> UpdateNotNull(T entity);

        /// <summary>
        /// 更新实体并指定更新与忽略的列
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="columns"></param>
        /// <param name="igcolumns"></param>
        /// <returns></returns>
        Task<int> Update(T entity, Expression<Func<T, object>> columns = null, Expression<Func<T, object>> igcolumns = null);

        /// <summary>
        /// 更新实体并指定更新与忽略的列
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="columns"></param>
        /// <param name="igcolumns"></param>
        /// <returns></returns>
        Task<int> Update(Expression<Func<T, bool>> expression = null, Expression<Func<T, T>> columns = null, Expression<Func<T, object>> igcolumns = null);

        Task<int> Update(int id, Expression<Func<T, object>> columns = null, Expression<Func<T, object>> igcolumns = null);

        /// <summary>
        /// 批量更新实体并指定更新与忽略的列
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="columns"></param>
        /// <param name="igcolumns"></param>
        /// <returns></returns>
        Task<int> Update(List<T> entity, Expression<Func<T, object>> columns = null, Expression<Func<T, object>> igcolumns = null);

        /// <summary>
        /// 根据SQL更新实体
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        Task<int> Update(string sql);

        #endregion 更新

        #region 更新2

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<int> Update<T2>(T2 entity) where T2 : ModelBase, new();

        /// <summary>
        /// 只更新不为null的字段
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<int> UpdateNotNull<T2>(T2 entity) where T2 : ModelBase, new();

        /// <summary>
        /// 更新实体并指定更新与忽略的列
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="columns"></param>
        /// <param name="igcolumns"></param>
        /// <returns></returns>
        Task<int> Update<T2>(T2 entity, Expression<Func<T2, object>> columns = null, Expression<Func<T2, object>> igcolumns = null) where T2 : ModelBase, new();

        /// <summary>
        /// 更新实体并指定更新与忽略的列
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="columns"></param>
        /// <param name="igcolumns"></param>
        /// <returns></returns>
        Task<int> Update<T2>(Expression<Func<T2, bool>> expression = null, Expression<Func<T2, T2>> columns = null, Expression<Func<T2, object>> igcolumns = null) where T2 : ModelBase, new();

        /// <summary>
        /// 更新实体并指定更新与忽略的列
        /// </summary>
        /// <typeparam name="T2"></typeparam>
        /// <param name="id"></param>
        /// <param name="columns"></param>
        /// <param name="igcolumns"></param>
        /// <returns></returns>
        Task<int> Update<T2>(int id, Expression<Func<T2, T2>> columns = null, Expression<Func<T2, object>> igcolumns = null) where T2 : ModelBase, new();

        /// <summary>
        /// 批量更新实体并指定更新与忽略的列
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="columns"></param>
        /// <param name="igcolumns"></param>
        /// <returns></returns>
        Task<int> Update<T2>(List<T2> entity, Expression<Func<T2, object>> columns = null, Expression<Func<T2, object>> igcolumns = null) where T2 : ModelBase, new();

        #endregion 更新2

        #region 软删除

        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<int> SoftDelete(int id);

        /// <summary>
        /// 软删除
        /// </summary>
        /// <typeparam name="T2"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<int> SoftDelete<T2>(int id) where T2 : ModelBase, new();

        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        Task<int> SoftDelete(List<int> ids);

        /// <summary>
        /// 软删除
        /// </summary>
        /// <typeparam name="T2"></typeparam>
        /// <param name="ids"></param>
        /// <returns></returns>
        Task<int> SoftDelete<T2>(List<int> ids) where T2 : ModelBase, new();

        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="wherexp"></param>
        /// <returns></returns>
        Task<int> SoftDelete(Expression<Func<T, bool>> wherexp);

        /// <summary>
        /// 软删除
        /// </summary>
        /// <typeparam name="T2"></typeparam>
        /// <param name="wherexp"></param>
        /// <returns></returns>
        Task<int> SoftDelete<T2>(Expression<Func<T2, bool>> wherexp) where T2 : ModelBase, new();

        #endregion 软删除

        #endregion 扩展方法

        /// <summary>
        /// 执行事务的方法，它接受可变数量的TransactionAction委托实例作为参数
        /// </summary>
        /// <param name="actions"></param>
        /// <returns></returns>
        Task ExecuteTransaction(params Func<Task>[] actions);
    }
}